<?php
namespace Kxdr\Controller;
use Think\Controller;
class HonorController extends ExtendController {		
		private $HTM = "KxdrHonoraryTitle";
		private $index_template = "index";
		public function index(){
			Vendor('Page');
			$tp = I("tp");
			
			$TOOL = D("ToolView");
			$tcfg = current($TOOL->where("`status`.`sid`=".$this->sid." AND `templet_code`='kxdr_honor'")->getField("status,version,cfg,tool_id"));
			if(!$tcfg){
				$this->success("您尚未开通插件：我的互动-荣誉，请先开通插件。",U("Shopmanage/chajian/index"));
				die();
			}
			if($tcfg["cfg"]){
				$tcfg["cfg"] = json_decode($tcfg["cfg"],true);
			}
			
			$where = $tp?" AND `status`=1":" AND `status`=0";
			$HT = M($this->HTM);
			$total = $HT->where("`sid`=".$this->sid.$where)->count("`id`");
			$Page = new \Page($total,20);
			$show = $Page->show();
			
			$honors = $HT->where("`sid`=".$this->sid.$where)->order("`score`")->limit($Page->firstRow.','.$Page->listRows)->getField("`id`,`score`,`title`");
			
			// var_dump($tcfg);die();
			$this->assign("tp",$tp);
			$this->assign("honors",$honors);
			$this->assign("tcfg",$tcfg);
			$this->assign("show",$show);
			$this->display($this->index_template);
		}		
		public function offical_preview(){
			$this->HTM="KxdrHonoraryTitleDefault";
			$this->index_template="offical_preview";
			$this->index();
		}
		public function delete_target_honor(){
			$id = I("id");
			$HT = M("KxdrHonoraryTitle");
			$status["status"]=1;
			$re = $HT->where("`sid`=".$this->sid." AND `id`=".$id)->save($status);
			if(!$re){
				$data["hasError"]=true;
				$data["error"]="修改失败！".$HT->getDbError();
			}else{
				$data["hasError"]=false;
			}
			$this->ajaxReturn($data);
		}
		public function edit_target_honor(){
			$data["id"]=I("id");
			$data["score"]=I("target_score");
			$data["title"]=I("target_title");
			$HT = M("KxdrHonoraryTitle");
			$re = $HT->save($data);
			if(!$re){
				$data["hasError"]=true;
				$data["error"]="修改失败！".$HT->getDbError();
			}
			$this->ajaxReturn($data);
		}
		public function renew_target_honor(){
			$data["id"]=I("id");
			$data["sid"]=$this->sid;
			$data["status"]=0;
			$HT = M("KxdrHonoraryTitle");
			$re = $HT->save($data);
			if(!$re){
				$data["hasError"]=true;
				$data["error"]="修改失败！".$HT->getDbError();
			}
			$this->ajaxReturn($data);
		}
		public function add_target_honor(){
			$data["sid"]=$this->sid;
			$data["score"]=I("target_score");
			$data["title"]=I("target_title");
			$HT = M("KxdrHonoraryTitle");
			$re["id"] = $HT->where("`sid`=".$this->sid." AND `score`=".$data["score"])->getField("`id`");
			$re["old_title"] = $HT->where("`sid`=".$this->sid." AND `title`='".$data["title"]."'")->getField("`id`");
			if($re["id"]){
				$data["hasError"]=true;
				$data["error"]="已经设置过这个分数了，不能重复。回收站内重复也不可以。";
			}elseif($re["old_title"]){
				$data["hasError"]=true;
				$data["error"].="已经有这个称号了，不能重复。回收站内重复也不可以。";
			}else{
				$data["id"]=$HT->add($data);
			}
			$this->ajaxReturn($data);
		}
		public function add_honor_title(){
			$data["honor_title"]["action"] = I("action");
			$data["honor_title"]["exact"] = I("exact");
			$data["honor_title"]["fault"] = I("fault");
			$json = json_encode($data);
			$TOOL = D("ToolView");
			$re = $TOOL->where("`status`.`sid`=".$this->sid." AND `templet_code`='kxdr_honor'")->save(Array("cfg"=>$json));
			$reda["hasError"] = !$re;
			$reda["error"] = $TOOL->getDbError();
			$this->ajaxReturn($reda);
		}		
		public function load_default_honor(){
			$gw = I("getway");
			$Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
			$re = $Model->execute("INSERT INTO `sm_kxdr_honorary_title` (`score`, `sid`, `title`, `status`) SELECT `score`, ".$this->sid.", `title`, `status`  FROM `sm_kxdr_honorary_title_default`");
			if(!$re){
				$data["hasError"]=true;
				$data["error"]=$Model->getDbError();
			}else{
				$data["hasError"]=false;
			}
			$this->ajaxReturn($data);
		}
}